package com.pajk.user.jmeter;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Utility {

	public static final String USER_SERVICE_REFERENCE_DEFINITION = "classpath:META-INF/spring/user-service-consumer.xml";

	public static final String USERIDS = "userIds";
	public static final String MOBILES = "mobiles";
	public static final String LOGEXT = ".log";
	public static final String ENCODING = "utf-8";

	public static final String JMETER_PREFIX_LABEL = "prefix";
	public static final String JMETER_OUTPUT_LABEL = "output";
	public static final String JMETER_REPEAT_LABEL = "repeat";

	public static final String JMETER_PREFIX_DEFAULT = "jMeter";
	public static final String JMETER_OUTPUT_DEFAULT = "D:\\xiangli\\pingan\\apache-jmeter-2.11\\bin\\workspace\\output\\";
	public static final int JMETER_REPEAT_DEFAULT = 100;

	public static int parseGroupId(String name) {
		int sI = name.lastIndexOf(' ');
		int eI = name.lastIndexOf('-');
		String groupId = name.substring(++sI, eI);
		return Integer.parseInt(groupId);
	}

	public static int parseThreadId(String name) {
		int sI = name.lastIndexOf('-');
		int eI = name.length();
		String threadId = name.substring(++sI, eI);
		return Integer.parseInt(threadId);
	}

	public static String[] populateOpenIdsCacheFromLog(String jMeterPrefix,
			String jMeterOutput, int jMeterRepeat, int groupId, int threadId)
			throws FileNotFoundException, IOException {

		StringBuffer sb = new StringBuffer();
		sb.append(jMeterOutput);
		sb.append(jMeterPrefix);
		sb.append("-Thread Group ");
		sb.append(new Integer(groupId).toString());
		sb.append('-');
		sb.append(new Integer(threadId).toString());
		sb.append('-');
		sb.append(Utility.USERIDS);
		sb.append(Utility.LOGEXT);
		String file = sb.toString();

		BufferedReader br = null;

		String[] openIds = new String[jMeterRepeat];

		try {
			br = new BufferedReader(new FileReader(file));
			String line = br.readLine();
			int i = 0;
			while (line != null && i < jMeterRepeat) {
				int index = line.lastIndexOf(':');
				openIds[i++] = line.substring(0, index);
				line = br.readLine();
			}
		} finally {
			if (br != null) {
				br.close();
				br = null;
			}
		}

		return openIds;
	}

	public static String[] populateUserIdsCacheFromLog(String jMeterPrefix,
			String jMeterOutput, int jMeterRepeat, int groupId, int threadId)
			throws FileNotFoundException, IOException {

		StringBuffer sb = new StringBuffer();
		sb.append(jMeterOutput);
		sb.append(jMeterPrefix);
		sb.append("-Thread Group ");
		sb.append(new Integer(groupId).toString());
		sb.append('-');
		sb.append(new Integer(threadId).toString());
		sb.append('-');
		sb.append(Utility.USERIDS);
		sb.append(Utility.LOGEXT);
		String file = sb.toString();

		BufferedReader br = null;

		String[] userIds = new String[jMeterRepeat];

		try {
			br = new BufferedReader(new FileReader(file));
			String line = br.readLine();
			int i = 0;
			while (line != null && i < jMeterRepeat) {
				int length = line.length();
				int index = line.lastIndexOf(':');
				userIds[i++] = line.substring(++index, length);
				line = br.readLine();
			}
		} finally {
			if (br != null) {
				br.close();
				br = null;
			}
		}

		return userIds;
	}

	public static String[] populateMobilesCacheFromLog(String jMeterPrefix,
			String jMeterOutput, int groupId, int threadId)
			throws FileNotFoundException, IOException {

		StringBuffer sb = new StringBuffer();
		sb.append(jMeterOutput);
		sb.append(jMeterPrefix);
		sb.append("-Thread Group ");
		sb.append(new Integer(groupId).toString());
		sb.append('-');
		sb.append(new Integer(threadId).toString());
		sb.append('-');
		sb.append(Utility.MOBILES);
		sb.append(Utility.LOGEXT);
		String file = sb.toString();

		BufferedReader br = null;

		String[] mobiles = null;

		try {
			br = new BufferedReader(new FileReader(file));
			String line = br.readLine();
			int index = line.lastIndexOf(' ');
			int length = Integer
					.valueOf(line.substring(++index, line.length()));
			mobiles = new String[length];
			int i = 0;
			while (line != null && i < length) {
				line = br.readLine();
				mobiles[i++] = line;
			}
		} finally {
			if (br != null) {
				br.close();
				br = null;
			}
		}

		return mobiles;
	}

}